package org.sea.mybator.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.sea.mybator.model.JdbcType;
import org.sea.mybator.model.TableDesc;
import org.sea.mybator.util.Constants;
import org.sea.mybator.util.Util;

public class CreateJdbcXml extends Create {

    public static void main(String[] args) {
        String tableNameFull = "T_ATTACH_UPLOAD";
        CreateJdbcXml test = new CreateJdbcXml();
        List<JdbcType> jdbcTypeList = test.init(tableNameFull);
        test.parse(jdbcTypeList, tableNameFull);
    }

    public boolean parse(List<JdbcType> jdbcTypeList, String tableNameFull) {

        VelocityContext context = new VelocityContext();

        String tableName = tableNameFull.replaceAll("T_", "");

        String className = Util.getDOName(tableName);
        String filePath = Constants.createFilePath + className + "Mapper.xml";
        String tprimaryKey = Constants.tprimaryKey;
        String tprimaryKeyCol = Constants.tprimaryKeyCol;
        String xmlPackagePath = Constants.xmlPackagePath;
        String repositoryPackagePath = Constants.repositoryPackagePath;
        String modelPackagePath = Constants.modelPackagePath;

        String resultMapId = Util.firstCharToUpperCase(tableName, "_");
        String ResultMapId = Util.firstLetterToUpperCase(resultMapId);

        context.put("entityPath", modelPackagePath + "." + className + "Entity");
        context.put("varchar", "VARCHAR");
        context.put("text", "TEXT");
        context.put("varchar", "VARCHAR");
        context.put("className", className);
        context.put("tableName", tableName);
        context.put("tableNameFull", tableNameFull);
        context.put("tprimaryKey", tprimaryKey);
        context.put("tprimaryKeyCol", tprimaryKeyCol);
        context.put("resultMapId", resultMapId + "Map");
        context.put("alias", Util.firstLetterToUpperCase(resultMapId));
        context.put("class", xmlPackagePath + "." + ResultMapId + "Entity");
        context.put("dserclass", repositoryPackagePath + "." + ResultMapId + "Mapper");

        context.put("jdbcTypeList", jdbcTypeList);

        context.put("queryByCondition", "query" + ResultMapId + "ByConditions");
        context.put("conditions", getQueryConditions(jdbcTypeList));

        Util.writeFile("mybatis_xml.vm", context, filePath);

        return false;
    }

    

    public List<JdbcType> init(String tableNameFull) {
        try {
            List<JdbcType> jdbcTypeList = new ArrayList<JdbcType>();
            Map<String, String> javaTypeMap = getDataTypeConversionProperty().getMap();

            List<TableDesc> tableDescs = getTableInfo(tableNameFull);
            for (TableDesc tableDesc : tableDescs) {
                String[] propertyName = Util.getPropertyName(tableDesc.getColumnName());
                JdbcType jdbcType = new JdbcType();
                if (propertyName.length == 2) {
                    jdbcType.setColumn(propertyName[1]);
                    jdbcType.setProperty(propertyName[1]);
                } else {
                    jdbcType.setColumn(tableDesc.getColumnName());
                    jdbcType.setProperty(propertyName[0]);
                }
                jdbcType.setComments(tableDesc.getRemarks());
                jdbcType.setJavaType(Util.getJavaType(tableDesc, javaTypeMap));
                jdbcType.setJdbcType(getJdbcType(tableDesc));
                jdbcType.setNotNullColumn("");
                jdbcType.setNullValue(getNullValue(tableDesc));
                jdbcType.setResultMap("");
                jdbcTypeList.add(jdbcType);
            }

            return jdbcTypeList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
